package com.burst.reptile.modular.web.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.web.entity.ZpAvatar;
import com.burst.reptile.modular.web.service.IZpAvatarService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 推荐头像列表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/web/avatar")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpAvatarController extends BaseController {

	private final IZpAvatarService avatarService;

	/**
	 * 获取所有头像
	 * 
	 * @return
	 */
	@GetMapping("/all")
	public Result<Object> getALL() {
		return Result.success(avatarService.list());
	}

	/**
	 * 获取所有头像
	 * 
	 * @return
	 */
	@GetMapping("/list")
	@PreAuthorize("@ss.hasPermi('zp:avatar:list')")
	public Result<Object> list() {
		List<ZpAvatar> list = avatarService.list();
        Map<String, List<ZpAvatar>> resultList = list.stream().collect(Collectors.groupingBy(ZpAvatar::getGroupId));
		return Result.success(resultList);
	}

	/**
	 * 新增头像
	 * 
	 * @return
	 */
	@PostMapping("/add")
	@PreAuthorize("@ss.hasPermi('zp:avatar:add')")
	public Result<Object> add(@Validated @RequestBody ZpAvatar avatar) {
		return Result.success(avatarService.add(avatar));
	}

	/**
	 * 修改头像
	 * 
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('zp:avatar:edit')")
	@PutMapping("/edit")
	public Result<Object> edit(@Validated @RequestBody ZpAvatar avatar) {
		return Result.success(avatarService.edit(avatar));
	}

	/**
	 * 删除头像
	 * 
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('zp:avatar:remove')")
	@DeleteMapping("/remove/{id}")
	public Result<Object> del(@PathVariable String id) {
		return Result.success(avatarService.del(id));
	}

}
